﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BMC.ARSystem;
using System.Collections;

namespace NRemedy.Test
{
    [TestClass]
    public class ARProxy_CTOR_Test
    {
        [TestMethod]
        public void ARProxy_CTOR_With_Null_Argument()
        {
            ARLoginContext loginContext = null;
            try
            {
                ARProxy<JustATestClass> server = new ARProxy<JustATestClass>(loginContext);
            }
            catch (ArgumentNullException ae)
            {
                Assert.AreEqual(ae.ParamName, "context");
            }
        }

        [TestMethod]
        public void ARProxy_CTOR_With_Argument_But_Null_ServerName()
        {
            ARLoginContext loginContext = new ARLoginContext();
            try
            {
                ARProxy<JustATestClass> server = new ARProxy<JustATestClass>(loginContext);
            }
            catch (ArgumentNullException ae)
            {
                Assert.AreEqual(ae.ParamName, "context.Server");
            }
        }

        [TestMethod]
        public void ARProxy_CTOR_With_Argument_But_Null_UserName()
        {
            ARLoginContext loginContext = new ARLoginContext();
            loginContext.Server = "testServer";
            try
            {
                ARProxy<JustATestClass> server = new ARProxy<JustATestClass>(loginContext);
            }
            catch (ArgumentNullException ae)
            {
                Assert.AreEqual(ae.ParamName, "context.User");
            }
        }

        [TestMethod]
        public void ARProxy_CTOR_With_Valid_Argument()
        {
            ARLoginContext loginContext = new ARLoginContext();
            Assert.AreEqual(loginContext.LoginStatus, ARLoginStatus.None);
            Assert.IsNull(loginContext.ServerInstance);
            loginContext.Init("testServer", "testUser", null);
            ARProxy<JustATestClass> server = new ARProxy<JustATestClass>(loginContext,new JustAnotherARServerFactory());
            Assert.AreEqual(loginContext.LoginStatus, ARLoginStatus.Success);
            Assert.IsNotNull(loginContext.ServerInstance);
        }

        [TestMethod]
        public void ARProxy_CTOR_With_Valid_Argument_UsingStatement()
        {
            using (ARLoginContext loginContext = new ARLoginContext("testServer", "testUser", null))
            {
                Assert.AreEqual(loginContext.LoginStatus, ARLoginStatus.None);
                Assert.IsNull(loginContext.ServerInstance);
                ARProxy<JustATestClass> server = new ARProxy<JustATestClass>(loginContext, new JustAnotherARServerFactory());
                Assert.AreEqual(loginContext.LoginStatus, ARLoginStatus.Success);
                Assert.IsNotNull(loginContext.ServerInstance);
            }

        }

        [TestMethod]
        public void ARProxy_CTOR_With_InValid_Argument_UsingStatement()
        {
            using (ARLoginContext loginContext = new ARLoginContext("testServer", "testUser", null))
            {
                Assert.AreEqual(loginContext.LoginStatus, ARLoginStatus.None);
                Assert.IsNull(loginContext.ServerInstance);
                try
                {
                    ARProxy<JustATestClass> server = new ARProxy<JustATestClass>(loginContext);
                }
                catch
                {
 
                }
                Assert.AreEqual(loginContext.LoginStatus, ARLoginStatus.Failed);
                Assert.IsNull(loginContext.ServerInstance);
            }

        }
    }

    internal class JustATestClass
    {
 
    }

    internal class JustAnotherARServerFactory : ARServerDefaultFactory
    {
        public override IARServer CreateARServer()
        {
            return new JustAMockARServerForLogin();
        }
    }

    internal class JustAMockARServerForLogin : IARServer
    {

        public void Login(string server, string user, string password, string authentication)
        {
            return;
        }


        public void Logout()
        {
            return;
        }


        public string CreateEntry(string form, FieldValueList fieldValueList)
        {
            throw new NotImplementedException();
        }

        public ArrayList GetListForm()
        {
            throw new NotImplementedException();
        }

        public ARForm GetForm(string name)
        {
            throw new NotImplementedException();
        }

        public ArrayList GetListField(string formName)
        {
            throw new NotImplementedException();
        }

        public Field GetField(string formName, uint id)
        {
            throw new NotImplementedException();
        }


        public void BeginBulkEntryTransaction()
        {
            throw new NotImplementedException();
        }

        public ArrayList CancelBulkEntryTransaction()
        {
            throw new NotImplementedException();
        }

        public ArrayList SendBulkEntryTransaction()
        {
            throw new NotImplementedException();
        }

        public void DeleteEntry(string form, string entryId, int deleteOption = 0)
        {
            throw new NotImplementedException();
        }

        public void SetEntry(string form, string entryId, FieldValueList fieldValueList)
        {
            throw new NotImplementedException();
        }

        public void SetEntryByQuery(string formName, string qualification, Server.SetEntryByQuery_NoMatchOption noMatchOption, Server.SetEntryByQuery_MultiMatchOption multiMatchOption, FieldValueList fieldValueList, bool useLocale = false)
        {
            throw new NotImplementedException();
        }

        public FieldValueList GetEntry(string form, string entryId, FieldIdList fieldIdList = null)
        {
            throw new NotImplementedException();
        }

        public ArrayList GetListEntry(string formName, string qualification, FieldIdList fieldIdList = null)
        {
            throw new NotImplementedException();
        }


        public bool ServerAlive
        {
            get { return true; }
        }
    }
}
